﻿Imports System.Configuration

Namespace Connection

    ''' <summary>
    ''' 環境要素
    ''' </summary>
    ''' <remarks></remarks>
    Public Class DatabaseElement
        Inherits ConfigurationElement

        ''' <summary>
        ''' 名前
        ''' </summary>
        <ConfigurationProperty("name", IsRequired:=True)> _
        Public Property Name() As String
            Get
                Return DirectCast(Me("name"), String)
            End Get
            Set(ByVal value As String)
                Me("name") = value
            End Set
        End Property

        ''' <summary>
        ''' アセンブリ要素
        ''' </summary>
        <ConfigurationProperty("assembly", IsRequired:=True)> _
        Public Property Assembly() As AssemblyElement
            Get
                Return DirectCast(Me("assembly"), AssemblyElement)
            End Get
            Set(ByVal value As AssemblyElement)
                Me("assembly") = value
            End Set
        End Property

        ''' <summary>
        ''' 接続文字列要素
        ''' </summary>
        <ConfigurationProperty("cn-string", IsRequired:=True)> _
        Public Property ConnectionString() As ConnectionStringElement
            Get
                Return DirectCast(Me("cn-string"), ConnectionStringElement)
            End Get
            Set(ByVal value As ConnectionStringElement)
                Me("cn-string") = value
            End Set
        End Property

        ''' <summary>
        ''' 接続生成クラスをインスタンスする
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function CreateConnector() As Connector
            Dim path As String = Me.Assembly.Path
            If IO.File.Exists(path) = False Then path = IO.Path.Combine(My.Application.Info.DirectoryPath, path)
            If IO.File.Exists(path) = False Then Throw New InvalidOperationException(String.Format("アセンブリファイルが見つかりません。(path:{0})", path))

            Dim asm As System.Reflection.Assembly = System.Reflection.Assembly.LoadFrom(path)
            Dim t As Type = (From el As Type In asm.GetTypes Where el.FullName = Me.Assembly.TypeName).FirstOrDefault
            If t Is Nothing Then Throw New InvalidOperationException(String.Format("アセンブリファイルに型がありません。(path:{0}, type:{1})", path, Me.Assembly.TypeName))

            'コネクターを生成、初期化
            Dim cr As Connector = t.GetConstructor(Type.EmptyTypes).Invoke(Nothing)
            cr.DatabaseName = Me.Name

            '接続文字列をセットします。コンバーターがある場合は変換処理を実行します。
            Dim cnString As String = Me.ConnectionString.Value
            If Me.ConnectionString.IsEncrypt AndAlso Connector.Decoder IsNot Nothing Then cnString = Connector.Decoder.Invoke(cnString)
            cr.ConnectionString = cnString

            '#90　DBMS名を管理する
            cr.DBMS = Kairyu.Connection.KairyuSectionHandlerFactory.Create.ExecutionEnvironment.DBMS

            Return cr
        End Function
    End Class

End Namespace

